package com.hxs.learn.action.core;


import com.hxs.learn.common.result.DataTableResult;
import com.hxs.learn.common.result.Result;
import com.hxs.learn.common.web.LoginInfo;
import com.hxs.learn.common.web.annotation.Login;
import com.hxs.learn.constants.core.web.ReqConst;
import com.hxs.learn.schedule.exception.ServiceException;
import com.hxs.learn.schedule.model.TaskInfo;
import com.hxs.learn.schedule.service.JobTaskService;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

/**
 * 定时任务管理
 *
 * @author xxjin
 */
@RestController
@RequestMapping(value = "core/schedule")
public class ScheduleAction extends AbstractViewAction {

    @Autowired
    private JobTaskService taskService;

    @RequiresRoles("admin")
    @GetMapping(ReqConst.REQ_INDEX)
    public ModelAndView index(@Login LoginInfo userInfo) {
        return createModelAndView(userInfo);
    }

    @RequiresRoles("admin")
    @GetMapping(ReqConst.REQ_QUERY_PAGE_DATA)
    public Result list(@Login LoginInfo userInfo) {
        List<TaskInfo> list = taskService.list();
        return DataTableResult.list(list);
    }

    @RequiresRoles("admin")
    @PostMapping(ReqConst.REQ_ADD_DATA)
    @ResponseBody
    public Result add(@Login LoginInfo userInfo, TaskInfo info) {
        try {
            taskService.addJob(info);
        } catch (ServiceException e) {
            return Result.error(e.getMessage());
        }
        return Result.ok();
    }

    @RequiresRoles("admin")
    @PostMapping(ReqConst.REQ_MODIFY_DATA)
    @ResponseBody
    public Result update(@Login LoginInfo userInfo, TaskInfo info) {
        try {
            taskService.edit(info);
        } catch (ServiceException e) {
            return Result.error(e.getMessage());
        }
        return Result.ok();
    }

    /**
     * 暂停
     *
     * @param info
     * @return
     */
    @RequiresRoles("admin")
    @PostMapping("pause")
    @ResponseBody
    public Result pause(@Login LoginInfo userInfo, TaskInfo info) {
        try {
            taskService.pause(info.getJobName(), info.getJobGroup());
        } catch (ServiceException e) {
            return Result.error(e.getMessage());
        }
        return Result.ok();
    }

    /**
     * 暂停
     *
     * @param info
     * @return
     */
    @RequiresRoles("admin")
    @PostMapping("resume")
    @ResponseBody
    public Result resume(@Login LoginInfo userInfo, TaskInfo info) {
        try {
            taskService.resume(info.getJobName(), info.getJobGroup());
        } catch (ServiceException e) {
            return Result.error(e.getMessage());
        }
        return Result.ok();
    }

    /**
     * 删除任务
     *
     * @param info
     * @return
     */
    @RequiresRoles("admin")
    @PostMapping("remove")
    @ResponseBody
    public Result remove(@Login LoginInfo userInfo, TaskInfo info) {
        try {
            taskService.delete(info.getJobName(), info.getJobGroup());
        } catch (ServiceException e) {
            return Result.error(e.getMessage());
        }
        return Result.ok();
    }

    @RequiresRoles("admin")
    @PostMapping("runNow")
    @ResponseBody
    public Result runNow(@Login LoginInfo userInfo, TaskInfo info) {
        try {
            taskService.runNow(info.getJobName(), info.getJobGroup());
        } catch (ServiceException e) {
            return Result.error(e.getMessage());
        }
        return Result.ok();
    }
}
